home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-13 | 3.5 KB | 125 lines | [TEXT/CWIE] |
- // ******************************************************************
- // program written by
- // Paul Baxter
- // MacHack'98
- //
- //
- // ******************************************************************
- #include <StdLib.h>
-
- #include "patch.h"
- #include "animate.h"
-
- // * ******************************************************************************
- // * AnimateWindow
- // * Bounce the window around the screen
- // * ******************************************************************************
- void AnimateWindow(WindowPtr theWindow, Point* savePt)
- {
- GrafPtr oldPort;
- Rect winRect, grayRect;
- short dx, dy, bounce, maxbounces, step;
-
- GetPort(&oldPort);
- SetPort(theWindow);
-
- savePt->h = savePt->v = 0;
- LocalToGlobal(savePt);
-
- winRect = ((WindowPeek)theWindow)->port.portRect;
- LocalToGlobal(&topLeft(winRect));
- LocalToGlobal(&botRight(winRect));
-
- SetPort(oldPort);
-
- if (((WindowPeek)theWindow)->visible) {
-
- grayRect = (*LMGetGrayRgn())->rgnBBox;
-
- if (RectInRect(&winRect, &grayRect)) {
- dx = myRandom(kMinSpeed, kStartSpeed);
- dy = myRandom(kMinSpeed, kStartSpeed);
- if (myRandom(kDirectionChangeLow, kDirectionChangeHi) > kDirectionThreshHold)
- dx = -dx;
- if (myRandom(kDirectionChangeLow, kDirectionChangeHi) > kDirectionThreshHold)
- dy =-dy;
-
- maxbounces = myRandom(kMinBounces, kMaxBounces);
- for (bounce = 0; bounce < maxbounces; bounce++) {
- do {
- MoveWindow(theWindow, winRect.left, winRect.top, false);
- OffsetRect(&winRect, dx, dy);
- } while (RectCompletelyInRect(&winRect, &grayRect, &dx, &dy));
- if (gSoundPlayer)
- CALL_SOUNDPROC(gSoundPlayer);
- OffsetRect(&winRect, dx, dy);
- }
- MoveWindow(theWindow, winRect.left, winRect.top, false);
- dx = (savePt->h - winRect.left) / kStepsBack;
- dy = (savePt->v - winRect.top) / kStepsBack;
- for (step = 0; step < kStepsBack; step++) {
- OffsetRect(&winRect, dx, dy);
- MoveWindow(theWindow, winRect.left, winRect.top, false);
- }
- }
- MoveWindow(theWindow, savePt->h, savePt->v, false);
- }
- }
-
- // * ******************************************************************************
- // * myRandom
- // * generate random numbers with a min and max
- // * ******************************************************************************
- short myRandom(short min, short max )
- {
- unsigned short rnd;
- long range, t;
-
- rnd = (unsigned short)rand();
- range = max - min;
- t = (rnd * range) / RAND_MAX;
- return (short)( t+min );
- }
-
- // * ******************************************************************************
- // * RectInRect
- // * Determine if 2 Rects intersect
- // * ******************************************************************************
- Boolean RectInRect(Rect* rect1, Rect* rect2)
- {
- return !(
- (rect1->bottom < rect2->top) ||
- (rect1->top > rect2->bottom) ||
- (rect1->right < rect2->left) ||
- (rect1->left > rect2->right)
- );
- }
-
- // * ******************************************************************************
- // * RectInRect
- // * Determine if rect1 is enclosed in rect2
- // * If not change direction and speed
- // * ******************************************************************************
- Boolean RectCompletelyInRect(Rect* rect1, Rect* rect2, short* dx, short* dy)
- {
- Boolean result;
-
- result = true;
- if ((rect1->bottom > rect2->bottom) ||
- (rect1->top< rect2->top)) {
-
- *dy = -(*dy);
- (*dy) *= myRandom(kMinSpeedMultiPlier, kMaxSpeedMultiplier);
- result = false;
-
- }
- if ((rect1->right > rect2->right) ||
- (rect1->left< rect2->left)) {
-
- *dx = -(*dx);
- *(dx) *= myRandom(kMinSpeedMultiPlier, kMaxSpeedMultiplier);
- result = false;
- }
- return result;
- }
-